package current;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * 模拟高并发
 */
public class CountDownLatchTest {
    private static final int NUM = 5;
    private static int count = 0;

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(NUM);
        System.out.println("Ready, Go!");
        waitAllArrrived(countDownLatch);
        try {
            // 阻塞等待
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Finished");
    }

    /**
     * 任务启动，到达一个计数减1，都到达后开启gate
     */
    private static void waitAllArrrived(CountDownLatch countDownLatch) {
        for (int i = 0; i < NUM; i++) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        bizCode();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        countDownLatch.countDown();
                    }
                }
            };
            t.start();
        }
    }

    /**
     * 业务调用，对数字进行加1操作
     */
    private static void bizCode() {
        for (int i = 0; i < 10; i++) {
            count++;
            System.out.println(Thread.currentThread().getName() + "This is bizCode--" + i + "count:" + count);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class CountDownLatchTest2 {
    private static final int NUM = 5;
    private static int count = 0;

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(NUM);
        System.out.println("Ready, Go!");
        waitAllArrrived(countDownLatch);
        System.out.println("Finished");
    }

    /**
     * 任务启动，到达一个计数减1，都到达后开启gate
     */
    private static void waitAllArrrived(CountDownLatch countDownLatch) {
        for (int i = 0; i < NUM; i++) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName() + "开始执行");
                        countDownLatch.await();
                        bizCode();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "执行完毕");
                }
            };
            t.start();
            countDownLatch.countDown();
        }
    }

    /**
     * 业务调用，对数字进行加1操作
     */
    private static void bizCode() {
        for (int i = 0; i < 10; i++) {
            count++;
            System.out.println(Thread.currentThread().getName() + "This is bizCode--" + i + "count:" + count);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class CyclicBarrierTest {

    private static final int NUM = 5;
    private static int count = 0;

    public static void main(String[] args) {
        Thread finishThread = new Thread() {
            @Override
            public void run() {
                System.out.println("Finished");
            }
        };
        System.out.println("Ready, Go!");
        CyclicBarrier cyclicBarrier = new CyclicBarrier(NUM, finishThread);
        waitAllArrrived(cyclicBarrier);
        System.out.println("Finished");
    }

    public static void waitAllArrrived(CyclicBarrier cyclicBarrier) {
        for (int i = 0; i < NUM; i++) {
            Thread t = new Thread() {
                @Override
                public void run() {
                    try {
                        System.out.println(Thread.currentThread().getName() + "开始执行");
                        cyclicBarrier.await();
                        bizCode();
                    } catch (InterruptedException | BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "执行完毕");
                }
            };
            t.start();
        }
    }

    /**
     * 业务调用，对数字进行加1操作
     */
    private static void bizCode() {
        for (int i = 0; i < 10; i++) {
            count++;
            System.out.println(Thread.currentThread().getName() + "This is bizCode--" + i + "count:" + count);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
